 const { request } = require("../../request/request");
// async
import regeneratorRuntime from '../../utils/regenerator-runtime/runtime.js'
// pages/classifyList/classifyList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productList:[],
    tabs:[
      {
        id:0, 
        name:'综合',
        isActive:true
      },
      {
        id:1, 
        name:'销量',
        isActive:false
      },
      {
        id:2, 
        name:' 价格',
        isActive:false
      }
    ],
    show:false
  },
 requestParams:{
  query:'',
  cid:'',
  pagenum:1,
  pagesize:10
 },
//  总页数
totalPages:1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log(options);
    that.requestParams.cid = options.cid||""; 
    this.requestParams.query = options.query||"";
    this.productList();
  },
// 定义点击事件
tabsClick(e){
 console.log(e)
 const index =e.detail.index;
 console.log(index);
 let tabs = this.data.tabs
 tabs.forEach((v, i)=>i===index?v.isActive=true : v.isActive=false)
 this.setData({
   tabs
 })
},
async productList(){
  let res = await request({url:'/goods/search', data:this.requestParams})
    console.log(res);
    let total = res.data.message.total;
      this.totalPages = Math.ceil(total/this.requestParams.pagesize)
    this.setData({
      productList:[...this.data.productList,...res.data.message.goods]
    });
    wx.stopPullDownRefresh()
},
// 上拉加载
onReachBottom(){
  console.log(this.requestParams.pagenum)
  console.log(this.totalPages)
  if(this.requestParams.pagenum>=this.totalPages){ 
   this,this.setData({
     show:true
   })
  }else{
    this.requestParams.pagenum++;
    this.productList();
  }
},
// 下拉刷新
onPullDownRefresh(){
  this.setData({
    productList:[]
  })
  this.requestParams.pagenum = 1;
  this.productList();
}
})